package com.mvc.business.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mvc.annotation.ClassAnnotation;
import com.mvc.annotation.ClassAnnotation.ClassAnnotationType;
import com.mvc.business.user.service.UserService;
import com.mvc.vo.User;

/**
 * REST接口风格实现用户CRUD
 * 自动组装JSON数据返回前台
 * @author HK
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Resource(name="userServiceImpl")
	private UserService userService;
	
	
	/**
	 * 显示用户列表
	 * @param model
	 * @return
	 */
	@ClassAnnotation(validate=ClassAnnotationType.YES)//此注解为自定义注解，在ClassAnnotationFilter中验证
	@RequestMapping("/list")
	public String list(Model model){
		List<User> list = userService.list();
		System.out.println(list.size());
		model.addAttribute("users",list);
		return "/user/list";
	}
	
	/**
	 * 查看当前用户信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String info(@PathVariable String id,Model model){
		model.addAttribute("user", userService.getUser(id));
		return "/user/info";
	}
	
	/**
	 * json格式返回当前用户信息
	 * springMVC自动组装返回的对象为json数据
	 * 需要导入jackson-all-1.9.1.jar以上版本
	 * 需要添加params="json"  值可变
	 * 需要添加@ResponseBody注解
	 * 访问此方法时链接后缀添加[?json]，如
	 * http://localhost:8080/mySpring/user/123?json 此链接访问的是使用json格式查看用户信息
	 * http://localhost:8080/mySpring/user/123 此链接访问的是查看当前用户信息info方法 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET,params="json")
	@ResponseBody
	public User info(@PathVariable String id){
		User u = userService.getUser(id);
		return null != u ? u : new User("aaa", "aaa", "aaa", 33, "aaab");
	}
	
	/**
	 * 修改用户前查询用户信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/{id}/update",method=RequestMethod.GET)
	public String update(@PathVariable String id,Model model){
		model.addAttribute("user", userService.getUser(id));
		return "/user/update";
	}
	
	/**
	 * 修改用户并返回到列表
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(@ModelAttribute User user){
		userService.update(user);
		return "redirect:/user/list";//重定向到list请求
	}
	
	/**
	 * 绑定用户前缀
	 * 使前台可以使用对象传值
	 * @param binder
	 */
	@InitBinder("user")
	public void bindUser(WebDataBinder binder){
		binder.setFieldDefaultPrefix("user.");
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}/delete",method=RequestMethod.GET)
	public String delete(@PathVariable String id){
		userService.delete(id);
		return "redirect:/user/list";
	}
	
	/**
	 * 新增用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(@ModelAttribute User user){
		userService.insert(user);
		return "redirect:/user/list";
	}
	
}
